// MifareOneDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "MifareOne.h"
#include "MifareOneDlg.h"
#include "umfh.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#pragma comment(lib,"S8ce.lib")


int icdev;
HWND   hwndCB;
HMENU  hMenu;
#define WS_NONAVDONEBUTTON WS_MINIMIZEBOX

NOTIFYICONDATA nd;
CString strAlllog;
BOOL  iWantExit;
BOOL  youCanExit = FALSE;

// CMifareOneDlg 对话框

CMifareOneDlg::CMifareOneDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMifareOneDlg::IDD, pParent)
	, m_rwDataStr(_T(""))
	, m_ReceiveString(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	showStr="";
}

void CMifareOneDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDT_DATA, m_rwDataStr);
	DDV_MaxChars(pDX, m_rwDataStr, 32);
	DDX_Text(pDX, IDC_RECEIVE, m_ReceiveString);
	DDX_Control(pDX, IDC_RECEIVE, m_CReStr);
}

BEGIN_MESSAGE_MAP(CMifareOneDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
	ON_WM_SIZE()
#endif
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BTN_READ, &CMifareOneDlg::OnBnClickedBtnRead)
	ON_BN_CLICKED(IDC_BTN_WRITE, &CMifareOneDlg::OnBnClickedBtnWrite)
	ON_EN_MAXTEXT(IDC_RECEIVE, &CMifareOneDlg::OnEnMaxtextReceive)
END_MESSAGE_MAP()


// CMifareOneDlg 消息处理程序

BOOL CMifareOneDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 设置此对话框的图标。当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	
	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CMifareOneDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
	if (AfxIsDRAEnabled())
	{
		DRA::RelayoutDialog(
			AfxGetResourceHandle(), 
			this->m_hWnd, 
			DRA::GetDisplayMode() != DRA::Portrait ? 
			MAKEINTRESOURCE(IDD_MIFAREONE_DIALOG_WIDE) : 
			MAKEINTRESOURCE(IDD_MIFAREONE_DIALOG));
	}
}
#endif

void CMifareOneDlg::showMsg(const char *msg)
{
	CString tmpstr(msg);
	CString strNL;

	UpdateData(TRUE);

	strNL.Format(_T("\r\n"));
	showStr=strNL+showStr;
	showStr=tmpstr+showStr;

	m_ReceiveString=showStr;
	UpdateData(FALSE);

}
void CMifareOneDlg::clearMsg()
{

	UpdateData(TRUE);

	showStr = "";
	m_ReceiveString="";

	UpdateData(FALSE);
}

void CMifareOneDlg::showString(const CString strmsg)
{

	CString strNL;

	UpdateData(TRUE);

	strNL.Format(_T("\r\n"));
	showStr=strNL+showStr;
	showStr=strmsg+showStr;

	m_ReceiveString=showStr;

	UpdateData(FALSE);
}

void CMifareOneDlg::UpdateMenuItems(BOOL fst)
{
	DWORD flag;

	if(fst)
		flag = MF_ENABLED;
	else 
		flag = MF_GRAYED;

//	EnableMenuItem(hMenu,ID_M1,flag);//Unable M1
//	EnableMenuItem(hMenu,ID_MENU_FCPU,flag);//Unalbe 4442
//	EnableMenuItem(hMenu,ID_MENU_S70,flag);

}

void CMifareOneDlg::GetCurCommPara(int* port, int* baud)
{
	CComboBox* m_comboPort = (CComboBox*)GetDlgItem(IDC_COMBO1);
	CComboBox* m_comboBaud = (CComboBox*)GetDlgItem(IDC_COMBO2);
	int idxBaud;
	CString strBaud;
	CString strMsg;


	*port = m_comboPort->GetCurSel();

	idxBaud = m_comboBaud->GetCurSel();
	m_comboBaud->GetLBText(idxBaud, strBaud);

	*baud = _ttoi(strBaud);

	strMsg.Format(L"port:%d, baud:%d", *port, *baud);
	MessageBox(strMsg, L"", MB_OK);

}

void CMifareOneDlg::OnBnClickedBtnRead()
{
	// TODO: 在此添加控件通知处理程序代码
 	unsigned char sector=15;//要操作的扇区
 	unsigned char block=60;//要操作的块号
 	unsigned char keybuf[7]={0xff,0xff,0xff,0xff,0xff,0xff};
// 	unsigned int cardtype;
// 	unsigned long cardtag;
 	unsigned long cardsnr;
// 	unsigned char cardSize;
 	unsigned char blkData[60];
 	unsigned char showData[256]={0};
 	unsigned char wtData[20]={0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66};
 	unsigned char NewkeyA[7]={0x11,0x22,0x33,0x44,0x55,0x66};// 新密码A
 	unsigned char CtrlBt[5]={0xff,0x07,0x80,0x69};//控制字节，建议不要改动这个值
 	unsigned char keyB[7]={0xff,0xff,0xff,0xff,0xff,0xff};//密码B,建议不要改动这个值
	int port, baud;

 	CString linstr;
	CString strError;
 
	GetCurCommPara(&port, &baud);
 
 	icdev=fw_init(port,baud);


	clearMsg();
 
 	if(icdev==-1)
 	{
 		showMsg("Link Device Error!");
 		return;							
 	}
 	else
 	{
 		showMsg("Link Device Ok!");
 		CString str;
 		str.Format(_T("Handle:%d"),icdev);
 		showString(str);
 	}

 	int st;
 		         

 
 	//==========================
 	//装载要操作的扇区的密码
 
 	st=fw_load_key(icdev,0,sector,keybuf);
 	if(st!=0)
 	{
 		showMsg("loadkey Error!");
 		CString str;//for test
 		str.Format(_T("%d"),st);
 		showString(str);
 		goto EXIT;
 	}
 	showMsg("loadkey OK!");
 
 
 	//=================================
 	//寻卡，获得卡号
 /*
 	st=fw_request(icdev,1,&cardtype);//1模式寻卡
     if(st!=0)
 	{
    	   showMsg("request Error!");
        goto EXIT;
 	}
 	else
 	{	
 //		showMsg("request Ok!");
 	}
 
 
 	st=fw_anticoll(icdev,0,&cardsnr);
 	if(st!=0)
 	{
 		showMsg("anticoll Error!");
 		goto EXIT;
 	}
 //	showMsg("anticoll OK!");
 	
 	linstr.Format(_T("Card Number: %u"),cardsnr);
 	showString(linstr);
 
 
 	st=fw_select(icdev,cardsnr,&cardSize);
 	if(st!=0)
 	{
 		showMsg("select Error!");
 		goto EXIT;
 	}
// 	showMsg("select OK!");

	*/

	//上面注释的3步可以用下面一步来集成

	st = fw_card(icdev,1,&cardsnr);
	if(st!=0)
	{
		showMsg("find card Error!");
		goto EXIT;
	}
	linstr.Format(_T("Card Number: %u"),cardsnr);
    showString(linstr);
 
 	//==========================
 	//验证要操作的扇区
 	//验证第sector扇区
 	st=fw_authentication(icdev,0,sector);
 	if(st!=0)
 	{
 		showMsg("authencation Error!");
 		goto EXIT;
 	}
 	showMsg("authencation OK!");
 	//==========================
 	//读卡数据测试
 	//读第secotr扇区的第block块的数据
 
 	st=fw_read(icdev,block,blkData);
 	if(st!=0)
 	{
 		showMsg("read Error!");
 		goto EXIT;
 	}
 	showMsg("read OK!");

 	hex_a(showData,blkData,32);

	m_rwDataStr = CString(showData);

	UpdateData(FALSE);


 /*	//================================
 	//把数据块当作是值块来操作
 	//初始化值
 	st=fw_initval(icdev,block,1000);//初始化为1000
 	if(st!=0)
 	{
 		showMsg("initval Error!");
 		goto EXIT;
 	}
 	showMsg("initval OK!");
 
 	//==================================
 	//增加值
 	st=fw_increment(icdev,block,80);
 	if(st!=0)
 	{
 		showMsg("increment Error!");
 		goto EXIT;
 	}
 	st=fw_transfer(icdev,block);
 	showMsg("increment OK!");
 
 	//==================================
 	//修改密码
 	//修改第sector扇区的密码
 
 	st=fw_changeb3(icdev,sector,NewkeyA,CtrlBt,0,keyB)||fw_halt(icdev);//第三个参数设为0
 	if(st!=0)
 	{
 		showMsg("changekey Error!");
 		goto EXIT;
 	}
 	showMsg("changekey OK!");
 
 	//改回原来的默认密码
 
 
 	st=fw_load_key(icdev,0,sector,NewkeyA);//先装载新密码
 	if(st!=0)
 	{
 		showMsg("New key load Error!");
 		goto EXIT;
 	}
 	showMsg("New key load OK!");
 
 	st=fw_card(icdev,1,&cardtag);//先寻卡
 	if(st!=0)
 	{
 		showMsg("Find card Error!");
 		goto EXIT;
 	}
 	st=fw_authentication(icdev,0,sector);//验证扇区1
 
 	if(st!=0)
 	{
 		showMsg("Authenication Error!");
 		goto EXIT;
 	}
 
 	st=fw_changeb3(icdev,sector,keybuf,CtrlBt,0,keyB)||fw_halt(icdev);//第三个参数设为0
 	if(st!=0)
 	{
 		showMsg("key has NOT been changed to old!");
 		goto EXIT;
 	}
 	showMsg("key has been changed to old!");*/

 	//设备蜂鸣
	st= fw_beep(icdev,5);
	if(st){
		showMsg("fw_beep_PDA error");
		return ;
	}
	else{
		showMsg("fw_beep_PDA OK");
	}
 
 	//==================================
 	//关闭通讯端口
 
 EXIT:
 	st=fw_exit(icdev);
 	if(st!=0)
 		showMsg("Close Device error!");
 	else
 		showMsg("Device has closed!");
 
 	return;
}

void CMifareOneDlg::OnBnClickedBtnWrite()
{
	// TODO: 在此添加控件通知处理程序代码
 	unsigned char sector=15;//要操作的扇区
 	unsigned char block=60;//要操作的块号
 	unsigned char keybuf[7]={0xff,0xff,0xff,0xff,0xff,0xff};
// 	unsigned int cardtype;
// 	unsigned long cardtag;
 	unsigned long cardsnr;
// 	unsigned char cardSize;
// 	unsigned char blkData[60];
 	unsigned char showData[256]={0};
	unsigned char wtData[20]={0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66};
	unsigned char wtDataHexStr[100]={0};
 	unsigned char NewkeyA[7]={0x11,0x22,0x33,0x44,0x55,0x66};// 新密码A
 	unsigned char CtrlBt[5]={0xff,0x07,0x80,0x69};//控制字节，建议不要改动这个值
 	unsigned char keyB[7]={0xff,0xff,0xff,0xff,0xff,0xff};//密码B,建议不要改动这个值
	int port, baud;
 
 	CString linstr;
	CString strError;
 
	GetCurCommPara(&port, &baud);
 
 	icdev=fw_init(port,baud);


	clearMsg();

 	if(icdev==-1)
 	{
 		showMsg("Link Device Error!");
 		return;							
 	}
 	else
 	{
 		showMsg("Link Device Ok!");
 		CString str;
 		str.Format(_T("Handle:%d"),icdev);
 		showString(str);
 	}
 
 	int st;
 		         

 	//==========================
 	//装载要操作的扇区的密码
 
 	st=fw_load_key(icdev,0,sector,keybuf);
 	if(st!=0)
 	{
 		showMsg("loadkey Error!");
 		CString str;//for test
 		str.Format(_T("%d"),st);
 		showString(str);
 		goto EXIT;
 	}
 	showMsg("loadkey OK!");
 
 
 	//=================================
 	//寻卡，获得卡号

	//上面注释的3步可以用下面一步来集成

	st = fw_card(icdev,1,&cardsnr);
	if(st!=0)
	{
		showMsg("find card Error!");
		goto EXIT;
	}
	linstr.Format(_T("Card Number: %u"),cardsnr);
    showString(linstr);
 
 	//==========================
 	//验证要操作的扇区
 	//验证第sector扇区
 	st=fw_authentication(icdev,0,sector);
 	if(st!=0)
 	{
 		showMsg("authencation Error!");
 		goto EXIT;
 	}
 	showMsg("authencation OK!");
 
 	//==============================
 	//写卡数据测试

	if( m_rwDataStr.GetLength() != 32)
	{
		showMsg("Data length to write Error!");
		goto EXIT;
	}


	wcstombs((char*)wtDataHexStr,m_rwDataStr,32);


	if(a_hex(wtData,wtDataHexStr,16)){
		showMsg("Data type to Write Error!");//要写的数据不符合16进制字符串格式
		goto EXIT;
	}

 
 	st=fw_write(icdev,block,wtData);
 	if(st!=0)
 	{
 		showMsg("write Error!");
 		goto EXIT;
 	}
 	showMsg("write OK!");

 	//设备蜂鸣
	st= fw_beep (icdev, 5);
	if(st){
		showMsg("fw_beep_PDA error");
		return ;
	}
	else{
		showMsg("fw_beep_PDA OK");
	}
 
 	//==================================
 	//关闭通讯端口
 
 EXIT:
 	st=fw_exit(icdev);
 	if(st!=0)
 		showMsg("Close Device error!");
 	else
 		showMsg("Device has closed!");
 
 	return;
}

LRESULT CMifareOneDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	// TODO: 在此添加专用代码和/或调用基类
	switch(message){
	case WM_COMMAND:
		
		if(wParam == IDOK)
		{//OK
			
			nd.cbSize = sizeof(NOTIFYICONDATA);
			nd.hWnd = m_hWnd;
			nd.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP;
			nd.uCallbackMessage = WM_SHELL_NOTIFYICON;
			nd.hIcon = m_hIcon;
			wcscpy(nd.szTip,L"Scanner");
			Shell_NotifyIcon(NIM_ADD,&nd);
			
			ShowWindow(SW_HIDE);
			
			return 0;
			
		}

		else if(wParam == IDCANCEL){ //X
			//	::MessageBox(NULL,L"MessageBox",L"cancel",MB_OK);
			
			if(icdev!=-1){
				fw_exit(icdev);
				icdev =-1;
			}
			
			
		}
		
		break;
		
	case WM_SHELL_NOTIFYICON:
		
		if(lParam == WM_LBUTTONDOWN)
		{
			ShowWindow(SW_SHOW);
			SetForegroundWindow();
			
		}
		
		break;
		
		
	default:
		break;
	}
	return CDialog::WindowProc(message, wParam, lParam);
}

void CMifareOneDlg::OnEnMaxtextReceive()
{
	// TODO: 在此添加控件通知处理程序代码
	m_CReStr.SetWindowText(_T(""));//设置里面数据为空
}
